import Define from "./common/Define";
import Common from "./common/Common";
import WXHelper from "./common/WXHelper";
import LDataCannonManager from "./datas/LDataCannonManager";
import TimeTaskManager from "./manager/TimeTaskManager";
import LDataSceneManager from "./datas/LDataSceneManager";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class UserInfo  {
    //当前关卡等级
    public static curLevel:number = 1;
    //发射子弹的数量
    public static bulletShootMaxCount:number = 3; //可以升级 子弹的发射数量
    //玩家的最大分数
    public static maxScore:number = 0;

    public static serverMaxScore:number = 0;
    //火力
    public static firePower:number = 1; //可以升级 增加分数
    //金币掉落的等级
    public static goldDropLevel:number = 1; //可以升级 增加金币掉落的产出
    //玩家当前金币
    public static curGold:number = 0;
    //离线等级
    public static offLineLevel:number = 0; //可以升级 
    //火力加层的概率
    public static firePowerAddRate:number = 1;

    //上次退出游戏时间
    public static lastQuitTime:number = 0;

    //当前使用的大炮id
    public static curUseCannonId:number
    //震动  开启震动
    public static brate:number = 1
    //每颗子弹增加的值默认是 1   100% = 2
    public static addFirePower:number = 1
    //这个是多出来的
    public static addFirePowerCount = 0;

    //当前保存数据的时间
    public static curWeek:number = -99;
    //玩家微信头像
    public static avatarUrl:string = "";
    //玩家的名字
    public static nickName:string = "";
    //玩家的openId
    public static openid:string = "";

    public static totalScore:number = 0;

    public static showAddHomeTag:number = 0

    public static curSceneId:number = 0;

    public static loadAllData(){

        //上次登录的时间
        let curWeek =  cc.sys.localStorage.getItem(Define.datakeyDate) //保存数据的周
        if(curWeek != null && curWeek != ""){
            this.curWeek = Number(curWeek)
        }else{
            this.curWeek = -99
        }

        console.log("curWeek = " +curWeek)

        //清空排行版分数
        let testDate = new Date();
        let week =  Common.getYearWeek(testDate.getFullYear(),testDate.getMonth()+1,testDate.getDate());
        if(week != this.curWeek && this.curWeek != -99){ //说明需要清空排行榜了
            this.maxScore = 0;
            //清空本地分数和服务器的分数
            this.saveMaxScore()
            WXHelper.instance.submitScore();
        }
        UserInfo.saveCannonById(1000,2)

        let gold = cc.sys.localStorage.getItem(Define.dataKeyGold);
        if(gold != null && gold != ""){
            this.curGold = Number(gold);
        }else{
            this.curGold = 0;
        }

        let level = cc.sys.localStorage.getItem(Define.dataKeyLevel)
        if(level != null && level != ""){
            this.curLevel = Number(level)
        }else{
            this.curLevel = 1;
        }
        let shootCount = cc.sys.localStorage.getItem(Define.dataKeyShootCount);
        if(shootCount != null && shootCount != ""){
            this.bulletShootMaxCount = Number(shootCount);
        }else{
            this.bulletShootMaxCount = 3;
        }

        let score = cc.sys.localStorage.getItem(Define.dataKeyMaxScore);
        if(score != null && score != ""){
            this.maxScore = Number(score);
        }else{
            this.maxScore = 0;
        }

        let firePower = cc.sys.localStorage.getItem(Define.dataKeyFirePower);
        if(firePower != null && firePower != ""){
            this.firePower = Number(firePower);
        }else{
            this.firePower = 0;
        }

        //添加的默认威力
        this.addFirePower = Math.floor((this.firePower*0.1 + 1));
        this.addFirePowerCount =  Math.floor(((this.firePower - 1)*0.1 + 1) - this.addFirePower)*this.bulletShootMaxCount


        let dropGoldLevel =  cc.sys.localStorage.getItem(Define.dataKeyGoldDropLevel)
        if(dropGoldLevel != null && dropGoldLevel != ""){
            this.goldDropLevel = Number(dropGoldLevel)
        }else{
            this.goldDropLevel = 1
        }

        let offLineLevel =  cc.sys.localStorage.getItem(Define.dataKeyOffLine)
        if(offLineLevel != null && offLineLevel != ""){
            this.offLineLevel = Number(offLineLevel)
        }else{
            this.offLineLevel = 0
        }
        //上次登录的时间
        let lastQuitTime =  cc.sys.localStorage.getItem(Define.dataKeyLastQuitTime)
        if(lastQuitTime != null && lastQuitTime != ""){
            this.lastQuitTime = Number(lastQuitTime)
        }else{
            this.lastQuitTime = 0
        }

        //上次登录的时间
        let totalScore =  cc.sys.localStorage.getItem(Define.dataKeyTotalScore)
        if(totalScore != null && totalScore != ""){
            this.totalScore = Number(totalScore)
        }else{
            this.totalScore = 0
        }   
        //记录上去使用大炮的id
        UserInfo.curUseCannonId =  cc.sys.localStorage.getItem(Define.dataKeyCurUseCannonId)
        if(totalScore != null && totalScore != ""){
            UserInfo.curUseCannonId = Number(UserInfo.curUseCannonId)
        }else{
            UserInfo.curUseCannonId = 0
        }   

        UserInfo.brate =  cc.sys.localStorage.getItem(Define.dataKeyBrate)
        if(totalScore != null && totalScore != ""){
            UserInfo.brate = Number(UserInfo.brate)
        }else{
            UserInfo.brate = 1
            this.saveBrateSate()
        } 

        let showAddHomeTag =  cc.sys.localStorage.getItem(Define.datakeyShowAddHomeTag)
        if(showAddHomeTag != null && showAddHomeTag != ""){
            UserInfo.showAddHomeTag = Number(showAddHomeTag)
        }else{
            UserInfo.showAddHomeTag = 0
        } 

        //当前场景
        let curScene =  cc.sys.localStorage.getItem(Define.datakeyScene)
        if(curScene != null && curScene != ""){
            UserInfo.curSceneId = Number(curScene)
        }else{
            UserInfo.curSceneId = 1000
        } 

        console.log("UserInfo.totalScore = " +UserInfo.totalScore)

        //老玩家如果已经超过这个这个等级自动获取
        UserInfo.handlerRewardCannons(Define.cannonDropGoldLevel);

        //老玩家如果已经超过这个分数
        UserInfo.handlerRewardCannons(Define.cannonTotalScore);
    }

    public static saveBrateSate(){
        cc.sys.localStorage.setItem(Define.dataKeyBrate,this.brate);
    }

    //保存金币
    public static saveGoldData(addCount:number = 0){
        this.curGold = this.curGold + addCount
        cc.sys.localStorage.setItem(Define.dataKeyGold,this.curGold);
    }
    //保存子弹发射的数量
    public static saveBulletShootMaxCount(addCount:number = 0){
        this.bulletShootMaxCount =  this.bulletShootMaxCount + addCount;
        cc.sys.localStorage.setItem(Define.dataKeyShootCount,UserInfo.bulletShootMaxCount);
    }
    //设置到下一个关卡
    public static saveNextLevel(){
        this.curLevel = this.curLevel + 1;
        cc.sys.localStorage.setItem(Define.dataKeyLevel,this.curLevel);
    }
    //保存火力
    public static saveFirePower(){
        this.firePower = this.firePower + 1;
        cc.sys.localStorage.setItem(Define.dataKeyFirePower,this.firePower);
        //添加的默认威力
        let tempPower = (this.firePower)*0.1 + 1
        this.addFirePower = Math.floor(tempPower);

        this.addFirePowerCount = (tempPower - this.addFirePower)*this.bulletShootMaxCount;
    }

    //保存掉落金币
    public static saveDropGoldLevel(){
        this.goldDropLevel = this.goldDropLevel + 1;
        cc.sys.localStorage.setItem(Define.dataKeyGoldDropLevel,this.goldDropLevel);
        UserInfo.handlerRewardCannons(Define.cannonDropGoldLevel);
    }

    //保存离线收入
    public static saveOffLineLevel(){
        this.offLineLevel = this.offLineLevel + 1;
        cc.sys.localStorage.setItem(Define.dataKeyOffLine,this.offLineLevel);
        UserInfo.handlerRewardCannons(Define.cannonOffLine);
    }

    //保存分数
    public static saveMaxScore(){
        let testDate = new Date();
        let week =  Common.getYearWeek(testDate.getFullYear(),testDate.getMonth()+1,testDate.getDate());
        cc.sys.localStorage.setItem(Define.dataKeyMaxScore,this.maxScore);
        cc.sys.localStorage.setItem(Define.datakeyDate,week.toString()); //保存数据的时间
    }
    //统计玩家最大分数
    public static saveTotalScore(){
        cc.sys.localStorage.setItem(Define.dataKeyTotalScore,this.totalScore);
        UserInfo.handlerRewardCannons(Define.cannonTotalScore);
    }
     //保存上次退出的时间
    public static saveLastQuitTime(){
        var testDate = new Date();
        cc.sys.localStorage.setItem(Define.dataKeyLastQuitTime,testDate.getTime());
    }

      //保存上次退出的时间
    public static saveShowAddHome(){
        cc.sys.localStorage.setItem(Define.datakeyShowAddHomeTag,1);
    }

    public static god(time){
        Common.isGod = true; //有2秒钟无敌时间
        TimeTaskManager.addTimeTask(time,function(){
            Common.isGod = false;
        }.bind(this),"reviveGame",1);
    }

    //当前使用的大炮id
    public static setCurUseCannonId(id:number){
        UserInfo.curUseCannonId = id
        cc.sys.localStorage.setItem(Define.dataKeyCurUseCannonId,UserInfo.curUseCannonId);
    }


    //当前使用的场景id
    public static setCurUseSceneId(id:number){
        UserInfo.curSceneId = id
        cc.sys.localStorage.setItem(Define.datakeyScene,UserInfo.curSceneId);
    }


    //获得当前场景的id
    public static getCurSceneIndex():number{
        let tempIndex:number = 0;
        for (let index = 0; index < LDataSceneManager.dataList.length; index++) {
            if(LDataSceneManager.GetDataById(index).saveID == UserInfo.curSceneId){
                tempIndex = index
            }
        }
        return tempIndex
    }

    ////////////////////////////////奖励大炮相关//////////////////////////////////////
    //奖励大炮
    public static handlerRewardCannons(getMode:number){
        for (let index = 0; index < LDataCannonManager.dataList.length; index++) {
            let data = LDataCannonManager.dataList[index];
            if(data.getmode == getMode){ //等于当前的获得方式
                if(!UserInfo.isOwnCannonById(data.cannonId)){ //没有奖励过
                    if(data.getmode == Define.cannonTotalScore){ //分数获取方式
                        if(UserInfo.totalScore >= data.scoreget){
                            UserInfo.addNewCannon(data.cannonId,1) //获得一个大炮
                        }
                    }
                    else if(data.getmode == Define.cannonDropGoldLevel){ //金币升级的方式
                        console.log("UserInfo.goldDropLevel = " + UserInfo.goldDropLevel + "   data.goldlevel = " + data.goldlevel);
                        if(UserInfo.goldDropLevel >= data.goldlevel){
                            UserInfo.addNewCannon(data.cannonId,1) //获得一个大炮
                        }
                    }else if(data.getmode == Define.cannonOffLine){ //离线等级获得大炮
                        console.log("UserInfo.offLineLevel = " + UserInfo.offLineLevel + "   data.offline = " + data.offline);
                        if(UserInfo.offLineLevel >= data.offline){
                            UserInfo.addNewCannon(data.cannonId,1) //获得一个大炮
                        }
                    }
                }
            }
        }

        //处理奖励场景
        for (let index = 0; index < LDataSceneManager.dataList.length; index++) {
            let data = LDataSceneManager.dataList[index];
            if(data.getmode == getMode){ //等于当前的获得方式
                if(!UserInfo.isOwnCannonById(data.saveID)){ //没有奖励过
                    if(data.getmode == Define.cannonTotalScore){ //分数获取方式
                        if(UserInfo.totalScore >= data.scoreget){
                            console.log("添加新的场景")
                            UserInfo.addNewCannon(data.saveID,1) //获得一个大炮
                        }
                    }
                    else if(data.getmode == Define.cannonDropGoldLevel){ //金币升级的方式
                        console.log("UserInfo.goldDropLevel = " + UserInfo.goldDropLevel + "   data.goldlevel = " + data.goldlevel);
                        if(UserInfo.goldDropLevel >= data.goldlevel){
                            UserInfo.addNewCannon(data.saveID,1) //获得一个大炮
                        }
                    }
                }
            }
        }
    }

    //保存一个大炮
    public static saveCannonById(id:number,state:number = 1){ //刚刚添加没有查看 2 添加之后已经查看
        let key = Define.dataKeyLastQuitTime + id.toString();
        cc.sys.localStorage.setItem(key,state);
    }
    //是否已经拥有这辆大炮
    public static isOwnCannonById(id:number){
        if(id == 0){ //默认给第一个大炮
            return true;
        }
        let cannon = cc.sys.localStorage.getItem(Define.dataKeyLastQuitTime + id.toString());
        if(cannon != null && cannon != ""){
            return true
        }
        return false
    }
    //获取大炮的状态
    public static getCannnonState(id:number){ //没有获取 1 新获取   2 获取之后并且查看
        let state = cc.sys.localStorage.getItem(Define.dataKeyLastQuitTime + id.toString());
        if(state != null && state != ""){
            state = Number(state)
        }else{
            state = 0
        }
        return state
    }
    //是否有获得的新大炮
    public static isHaveNewCannon(){
        for (let index = 0; index < LDataCannonManager.dataList.length; index++) {
            let state:number = UserInfo.getCannnonState(LDataCannonManager.dataList[index].cannonId)
            if(state == 1){
                return true
            }
        }
        return false
    }
    //是否有新的场景
    public static isHaveNewScene(){
        for (let index = 0; index < LDataSceneManager.dataList.length; index++) {
            let state:number = UserInfo.getCannnonState(LDataSceneManager.dataList[index].saveID)
            if(state == 1){
                return true
            }
        }
        return false
    }
    //添加一个新的大炮
    public static addNewCannon(id:number,state:number = 1){
        UserInfo.saveCannonById(id,state);
    }   
    //清空所有数据
    public static clearAllDatas(){
        cc.sys.localStorage.clear();
    }
}
